/*
 * Created on Mar 30, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.scohen.juploadr.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.scohen.juploadr.Messages;

/**
 * @author steve
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class MessageUtils {

    public static void showErrorMessage(String message) {
        showMessage(message,
                Messages.getString("juploadr.ui.label.error"), SWT.ICON_ERROR); //$NON-NLS-1$
    }

    public static void showErrorMessage(String[] messages) {
        StringBuffer message = new StringBuffer();
        for (int i = 0; i < messages.length; i++) {
            message.append(messages[i]);
            message.append("\n"); //$NON-NLS-1$
        }
        showErrorMessage(message.toString());
    }

    public static void showInfoMessage(String message) {
        showMessage(
                message,
                Messages.getString("juploadr.ui.label.information"), SWT.ICON_INFORMATION); //$NON-NLS-1$
    }

    public static boolean showMessage(final String message, final String type,
            final int iconType) {
        final Display disp = ReusableUIFactory.getInstance().getDisplay();
        MessageUtils.MessageBoxThread t = new MessageUtils().new MessageBoxThread(
                disp, iconType, type, message);

        disp.syncExec(t);
        return t.isConfirmed();
    }

    private class MessageBoxThread extends Thread {
        private String message;
        private Display display;
        private int iconType;
        private String title;
        private int buttonId;

        public MessageBoxThread(Display display, int type, String message,
                String title) {
            super();
            // TODO Auto-generated constructor stub
            this.display = display;
            iconType = type;
            this.message = message;
            this.title = title;
        }

        public void run() {
            MessageBox error = new MessageBox(display.getShells()[0], iconType);
            error.setMessage(title);
            error.setText(message);
            buttonId = error.open();
        }

        public boolean isConfirmed() {
            return buttonId == SWT.OK || buttonId == SWT.YES;
        }
    }
}